﻿using Nutshell.Components.Models;
using Nutshell.Data;
using Nutshell.Distributing;
using Nutshell.Messaging.Models;
using Nutshell.NetMQ;
using Nutshell.NetMQ.Models;

namespace Nutshell.DLS.Business
{
        public class GlobalManager : NotifyPropertyChangedObject
        {
                #region 构造函数

                private GlobalManager()
                {
                }

                #endregion

                #region 单例

                /// <summary>
                ///         单例
                /// </summary>
                public static readonly GlobalManager Instance = new GlobalManager();

                #endregion

                public NetMQReceiver Receiver { get; private set; }

                public ReceiveSite<LogMessageModel> ReceiveSite { get; private set; }

                public LogMessageCollection Collection { get; private set; }

                public void Start()
                {
                        Collection = new LogMessageCollection();
                        Collection.Start();

                        LoadReceiver();
                        LoadReceiveSite();

                        ReceiveSite.Dispatched += (o, args) => Collection.Insert(args.Value);
                        ReceiveSite.Start();
                }

                private void LoadReceiver()
                {
                        Receiver = new NetMQReceiver();
                        Receiver.LoadFromXML<NetMQModel>(@"配置\Receiver.config");
                }

                private void LoadReceiveSite()
                {
                        ReceiveSite = new ReceiveSite<LogMessageModel>(Receiver);
                        ReceiveSite.LoadFromXML<WorkerModel>(@"配置\ReceiverSite.config");
                }


                public void Stop()
                {
                        ReceiveSite.Stop();
                        Collection.Stop();
                }
        }
}